//
//  SSNetworkTools.m
//
//  Created by Esther on 2016/12/15.
//  Copyright © 2016年 Esther. All rights reserved.
//

#import "SSNetworkTools.h"
#import <AFNetworking.h>
#import "SSProgressHUD.h"

@implementation SSNetworkTools{
    AFHTTPSessionManager *_mgr;
    AFHTTPSessionManager *_focusMgr;
    
}

#pragma mark -
#pragma mark - 实现一个NetworkTools单例
static id _instance;
+ (instancetype)shared{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
        
    });
    return _instance;
}

#pragma mark -
#pragma mark - 初始化init方法
- (instancetype)init{
    self = [super init];
    if (self) {
        #pragma mark - 单个AFHTTPSessionManager
//        NSString *baseURL = @"http://123.57.141.249:8080/beautalk/";
//        @"http://iosapi.itcast.cn/loveBeen/focus.json.php"
        NSString *baseURL = @"http://iosapi.itcast.cn/";
        NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
        _mgr = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:baseURL] sessionConfiguration:config];
        _mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html",@"text/plain", nil];//[NSSet setWithObject:@"text/plain"];
        _mgr.requestSerializer = [AFJSONRequestSerializer serializer];
//        _mgr.requestSerializer = [AFHTTPRequestSerializer serializer];
        
        //========================================/
        
        _focusMgr = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:baseURL] sessionConfiguration:config];
        _focusMgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html",@"text/plain", nil];//[NSSet setWithObject:@"text/plain"];
        _focusMgr.requestSerializer = [AFJSONRequestSerializer serializer];
//        _focusMgr.requestSerializer = [AFHTTPRequestSerializer serializer];
        
        
//        _mgr.requestSerializer.timeoutInterval = 10.0;
        
    }
    return self;
}

#pragma mark -
#pragma mark - 发送GET请求
- (void)ss_getLoadData:(NSString *)urlString completeBlock:(void (^)(id, NSError *))completeBlock{
    [SSProgressHUD ss_show];
    [_mgr GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [SSProgressHUD ss_dismiss];
        // 请求成功
        if (completeBlock) {
            completeBlock(responseObject,nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SSProgressHUD ss_dismiss];
        // 请求错误
        if (completeBlock) {
            NSLog(@"%@",error);
            completeBlock(nil,error);
            
        }
    }];
}

- (void)ss_getLoadData:(NSString *)urlString parameters:(id)parameters completeBlock:(void (^)(id, NSError *))completeBlock{
    [SSProgressHUD ss_show];
    [_mgr GET:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [SSProgressHUD ss_dismiss];
        // 请求成功
        if (completeBlock) {
            completeBlock(responseObject,nil);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SSProgressHUD ss_dismiss];
        // 请求错误
        if (completeBlock) {
            NSLog(@"%@",error);
            completeBlock(nil,error);
            
        }
    }];
}

#pragma mark -
#pragma mark - 发送POST请求
- (void)ss_postLoadData:(NSString *)urlString parameters:(id)parameters completeBlock:(void (^)(id, NSError *))completeBlock{
    [SSProgressHUD ss_show];
    [_mgr POST:urlString
        parameters:parameters
          progress:nil
           success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {  //成功
               [SSProgressHUD ss_dismiss];
               if (completeBlock) {
                   completeBlock(responseObject,nil);
               }
           }
           failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
               [SSProgressHUD ss_dismiss];
               // 失败
               if (completeBlock) {
                   NSLog(@"%@",error);
                   completeBlock(nil,error);
                   
               }
           }];
}


#pragma mark -
#pragma mark - 发送POST请求
- (void)ss_postFocusData:(NSString *)urlString parameters:(id)parameters completeBlock:(void (^)(id, NSError *))completeBlock{
    [SSProgressHUD ss_show];
    [_focusMgr POST:urlString
    parameters:parameters
      progress:nil
       success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {  //成功
           [SSProgressHUD ss_dismiss];
           if (completeBlock) {
               completeBlock(responseObject,nil);
           }
       }
       failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
           [SSProgressHUD ss_dismiss];
           // 失败
           if (completeBlock) {
               NSLog(@"%@",error);
               completeBlock(nil,error);
               
           }
       }];
}
@end
